package com.xing.controller;

import com.xing.entity.pojo.SystemRole;
import com.xing.entity.pojo.SystemUser;
import com.xing.entity.vo.AuthorityUser;
import com.xing.service.SystemFunctionService;
import com.xing.service.SystemRoleService;
import com.xing.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SystemFunctionService systemFunctionService;

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemRoleService systemRoleService;

    @RequestMapping("findUserAll")
    public String findUserAll(SystemUser systemUser, Model model,@RequestParam(value = "role",required = false) String role){
        if(role!=null){
            model.addAttribute("role",role);
        }
        List<SystemUser> users = systemUserService.findAll(systemUser);
        model.addAttribute("users",users);
        model.addAttribute("systemUser",systemUser);
        return "user/user";
    }

    @RequestMapping("/findByUser/{userId}")
    public String findByUser(@PathVariable("userId") String userId,Model model){
        List<SystemRole> roles = systemRoleService.findNameAll();
        model.addAttribute("roles",roles);
        if(userId.equals("add")){
            return "user/addUser";
        }else {
            SystemUser user = systemUserService.selectByPrimaryKey(userId);
            model.addAttribute("user",user);
            return "user/updateUser";
        }
    }

    @PostMapping("/addUser")
    @ResponseBody
    public String addUser(SystemUser systemUser){
        systemUser.setUserId(UUID.randomUUID().toString());
        int i = systemUserService.insertSelective(systemUser);
        if(i>0){
            return "添加成功！";
        }else{
            return "添加失败！";
        }
    }
    @PostMapping("/updateUser")
    @ResponseBody
    public String updateUser(SystemUser systemUser){
        int i = systemUserService.updateByPrimaryKeySelective(systemUser);
        if(i>0){
            return "修改成功！";
        }else{
            return "修改失败！";
        }
    }
    @RequestMapping("/deleteUser/{userId}")
    public String deleteUser(@PathVariable("userId") String userId){
        systemUserService.deleteByPrimaryKey(userId);
        return "redirect:/user/findUserAll";
    }

    @RequestMapping("/deleteSelectedUser")
    @ResponseBody
    public String  deleteSelectedUser(@RequestParam("ids") String[] ids){
        if(systemUserService.deleteSelectedUser(ids)){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @RequestMapping("/login")
    public String login(SystemUser user, Model model, HttpSession session){
        SystemUser login = systemUserService.findUserAll(user);
        if(login!=null){
            List<AuthorityUser> list = systemFunctionService.findByRoleID(login.getRoleId());
            session.setAttribute("LoginName",login.getUserTrueName());
            session.setAttribute("userId",login.getUserId());
            session.setAttribute("list",list);
            //model.addAttribute("list",list);
            return  "main";
        }else {
            model.addAttribute("msg","用户名和密码错误");
            return "login";
        }

    }

    @RequestMapping("/gologin")
    public String login(){
        return "login";
    }



    @RequestMapping("/username")
    @ResponseBody
    public String username(String name){
        System.out.println(name);
        String flag="";
        SystemUser byName = systemUserService.findByName(name);
        if(byName!=null){
            flag="ture";
        }else {
            flag="false";
        }
        return flag;
    }

    @GetMapping("/updateRole")
    public String updateRole(@RequestParam(value = "roleId") String roleId,@RequestParam("userId") String userId){
        systemUserService.updateRoleId(roleId, userId);
        return  "redirect:/user/findUserAll";
    }

}
